package com.ny.service;

import com.ny.device.Devices;
import com.ny.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;
    @Override
    public void deleteByDeviceId(int deviceId) {
        deviceMapper.deleteByDeviceId(deviceId);
    }

    @Override
    public List<Devices> findByNameContaining(String name) {
        return null;
    }

    @Override
    public List<Devices> findByNameLike(String name) {
        return deviceMapper.findByNameContaining(name);
    }


    @Override
    public List<Devices> findAll() {
        return deviceMapper.findAll();
    }
    @Override
    public List<Devices> findByName(String name) {
        return deviceMapper.findByName(name);
    }

    @Override
    public Devices addDevice(Devices device) {
        // MongoDB会自动生成_id，这里直接保存
        return deviceMapper.save(device);
    }

    @Override
    public Devices updateDeviceNameById(int deviceId, String newName) {
        Optional<Devices> optionalDevice = deviceMapper.findByDeviceId(deviceId);
        if (optionalDevice.isPresent()) {
            Devices device = optionalDevice.get();
            device.setName(newName); // 修改名称
            return deviceMapper.save(device); // 保存修改
        }
        throw new RuntimeException("设备ID不存在：" + deviceId);
    }
}